package com.sxm.yiwei.tools;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.AppManager;
import com.sxm.yiwei.activitis.BaseActivity;
import com.sxm.yiwei.activitis.InstallPsgActivity;
import com.sxm.yiwei.activitis.MineAccountJoinMoney;
import com.sxm.yiwei.activitis.MinePayPasswordActivity;
import com.sxm.yiwei.beans.pinche.Route;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.demo.PayResult;
import com.sxm.yiwei.pinche.CompleteActivity;
import com.sxm.yiwei.pinche.HostOrdersActivity;
import com.sxm.yiwei.pinche.PassengerOrdersActivity;
import com.sxm.yiwei.pinche.ReleaseActivity;
import com.sxm.yiwei.utils.AESUtil;
import com.sxm.yiwei.utils.BottomDialogOnclickListener;
import com.sxm.yiwei.utils.BottomDialogView;
import com.sxm.yiwei.utils.DialogUtil;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

public class BookPayActivity extends BaseActivity implements View.OnClickListener{
    private String TAG ="Pay";
    private RelativeLayout alipaytext;
    private RelativeLayout weixinpaytext;
    private CheckBox alibox;
    private CheckBox weixinbox;
    private Button bookpay;
    private RelativeLayout ali;
    private RelativeLayout weixin;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String data;
    private String word;
    private String amount;
    private String makeupId;
    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PayResult payresult = new PayResult((Map<String, String>)msg.obj);
            String resultStatus = payresult.getResultStatus();
            if (resultStatus.equals("9000")){
                Toast.makeText(BookPayActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                if (from.equals(Constant.PASSENGER_PUBLISH_PAY)||from.equals(Constant.HOST_PUBLISH_PAY)||from.equals(Constant.CHANGE_PAY)||from.equals(Constant.PASSENGER_ROB_PAY)||from.equals(Constant.HOST_ROB_PAY)){
                    Intent intent = new Intent(BookPayActivity.this, CompleteActivity.class);
                    intent.putExtra("type",completeType);
                    intent.putExtra("text_1",text_1);
                    intent.putExtra("text_2",text_2);
                    Log.i("complete","type="+completeType+"  text1"+text_1+"  text2"+text_2);
                    startActivity(intent);
                }
            }else if (resultStatus.equals("8000")){
                Toast.makeText(BookPayActivity.this, "支付处理中", Toast.LENGTH_SHORT).show();

            }else if (resultStatus.equals("4000")){
                Toast.makeText(BookPayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();

            }else if (resultStatus.equals("5000")){
                Toast.makeText(BookPayActivity.this, "订单重复提交", Toast.LENGTH_SHORT).show();

            }else if (resultStatus.equals("6002")){
                Toast.makeText(BookPayActivity.this, "请检查你的网络是否启", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(BookPayActivity.this, "请稍等，支付结果获取中", Toast.LENGTH_SHORT).show();

            }
        }
    };


    private String alipay;
    private String yivipay;
    private TextView bookpaymoney;
    private TextView bookpayno;
    private String from;
    private String orderCode;
    private TextView alipaymoney;
    private TextView weixinpaymoney;
    private ImageView bookpayback;
    private AlertDialog progress;
    private WindowManager.LayoutParams params;
    private ImageView backmain;
    private ArrayList<Route> serviceList;
    private int completeType;
    private String text_1;
    private String text_2;
    private String departTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_pay);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid","1");
        userId = sharedPreferences.getString("userId", "0");
        token = sharedPreferences.getString("token", "2");
        Intent intent = getIntent();
        orderCode = intent.getStringExtra("orderCode");
        from = intent.getStringExtra("from");
        amount = intent.getStringExtra("amount");
        makeupId = intent.getStringExtra("id");
        alipay = intent.getStringExtra("alipay");
        yivipay = intent.getStringExtra("yivipay");
        if (from.equals(Constant.CHANGE_PAY)){
            serviceList = intent.getParcelableArrayListExtra("serviceList");
        }else if (from.equals(Constant.PASSENGER_PUBLISH_PAY)||from.equals(Constant.HOST_PUBLISH_PAY)||from.equals(Constant.CHANGE_PAY)||from.equals(Constant.PASSENGER_ROB_PAY)||from.equals(Constant.HOST_ROB_PAY)){
            completeType = intent.getIntExtra("type",-1);
            text_1 = intent.getStringExtra("text_1");
            text_2 = intent.getStringExtra("text_2");
        }else if (from.equals(Constant.PASSENGER_PUBLISH_PAY)||from.equals(Constant.HOST_PUBLISH_PAY)){
            departTime = intent.getStringExtra("departTime");
        }

        initBookPay();
        type=2;
        alipaytext.setVisibility(View.VISIBLE);
        weixinpaytext.setVisibility(View.INVISIBLE);
        alipaymoney.setText(amount);
        weixinbox.setChecked(false);
        alibox.setChecked(true);
    }


    private void initBookPay() {
        backmain = (ImageView) findViewById(R.id.bookpay_backmain);
        bookpayback = (ImageView) findViewById(R.id.book_pay_back);
        bookpaymoney = (TextView) findViewById(R.id.bookpay_money);
        bookpayno = (TextView) findViewById(R.id.bookpay_no);
        alipaytext = (RelativeLayout) findViewById(R.id.bookpay_alipaytext);
        weixinpaytext = (RelativeLayout) findViewById(R.id.bookpay_weixinpaytext);
        alibox = (CheckBox) findViewById(R.id.bookpay_alibox);
        weixinbox = (CheckBox) findViewById(R.id.bookpay_weixinbox);
        ali = (RelativeLayout) findViewById(R.id.ali);
        weixin = (RelativeLayout) findViewById(R.id.weixin);
        alipaymoney = (TextView) findViewById(R.id.bookpay_alipaymoney);
        weixinpaymoney = (TextView) findViewById(R.id.bookpay_weixinmoney);
        bookpaymoney.setText(amount);
        bookpayno.setText(orderCode);
        ali.setOnClickListener(this);
        weixin.setOnClickListener(this);
        bookpay = (Button) findViewById(R.id.bookpay);
        bookpay.setOnClickListener(this);
        bookpayback.setOnClickListener(this);
        backmain.setOnClickListener(this);

    }
    private int type;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bookpay_backmain:
                startActivity(new Intent(BookPayActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.book_pay_back:

                finish();
                break;
            case R.id.weixin:
                type=1;
                alipaytext.setVisibility(View.INVISIBLE);
                weixinpaytext.setVisibility(View.VISIBLE);
                weixinpaymoney.setText(amount);
                weixinbox.setChecked(true);
                alibox.setChecked(false);
                break;
            case R.id.ali:
                type=2;
                alipaytext.setVisibility(View.VISIBLE);
                weixinpaytext.setVisibility(View.INVISIBLE);
                alipaymoney.setText(amount);
                weixinbox.setChecked(false);
                alibox.setChecked(true);
                break;
            case R.id.bookpay:
                // 此处支付订单
                AlertDialog.Builder builder = new AlertDialog.Builder(BookPayActivity.this,R.style.NoBackGroundDialog);
                View view = LayoutInflater.from(BookPayActivity.this).inflate(R.layout.progress_dialog, null);
                ProgressBar diapro = (ProgressBar) view.findViewById(R.id.pro_dia);
                builder.setView(view);
                progress = builder.create();
                progress.setCancelable(false);
                progress.show();
                if (type==1){
                    //钱包支付首先判断是否设置支付密码
                    getpaystates();
                    // 钱包
                  //  showDialog(yivipay);
                }else if (type==2){
                    // 支付宝
                    /*if (from.equals(Constant.PASSENGER_PUBLISH_PAY)||from.equals(Constant.HOST_PUBLISH_PAY)){
                        if (Long.parseLong(departTime)<= SystemClock.currentThreadTimeMillis()){
                            Toast.makeText(this,"当前时间超过出发时间，请重新发布",Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
*/
                    sendBook(amount,alipay);

                }

                break;
        }
    }
        // 判断是否设置支付密码
    private void getpaystates() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.PAYWORDSTATES);
        params.setHeader("Content-Type","application/json");
        params.setHeader("token", token);
        params.setHeader("appId", appId);
        params.setHeader("userId", userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                JSONObject obj1 = JSON.parseObject(data);
                String isSetPassword = obj1.getString("isSetPassword");

                if (isSetPassword.equals("true")){
                    progress.dismiss();
                    showDialog(yivipay);
                }else {
                    progress.dismiss();
                    startActivity(new Intent(BookPayActivity.this,InstallPsgActivity.class));
                    Toast.makeText(BookPayActivity.this, "请设置支付密码", Toast.LENGTH_SHORT).show();
                }
               
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progress.dismiss();
                finish();
                Toast.makeText(BookPayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    // 支付宝支付方法
    public void sendBook(String money, final String url) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+url);
        params.setHeader("Content-Type","application/json");
        params.setHeader("token", token);
        params.setHeader("appId", appId);
        params.setHeader("userId", userId);
        final JSONObject j = new JSONObject();

        // 区别对待不同页面
        if (from.equals("between")){
            j.put("payType","07");
            j.put("orderId",makeupId);
        }else if (from.equals("add")){
            j.put("payType","06");
            j.put("makeupId",makeupId);
        }else if (from.equals("hot")){
            j.put("payType","08");
            j.put("orderId",makeupId);
        }else if (from.equals("help")){
            j.put("payType","09");
            j.put("orderId",makeupId);
        }else if (from.equals("reward")){
            j.put("payType","11");
            j.put("rewardId",makeupId);
        }else if (from.equals("buyservice")){
            j.put("payType","12");
            j.put("orderId",makeupId);
        }else if (from.equals("fwadd")){
            j.put("payType","13");
            j.put("makeupId",makeupId);
        }else if (from.equals("service")){
            j.put("payType","02");
            j.put("shopId",makeupId);
        }else if (from.equals(Constant.PASSENGER_PUBLISH_PAY)){//乘客发布
            j.put("payType",""+Constant.PASSENGER_PUBLISH_FLAG);
            j.put("serviceId",makeupId);
        }else if (from.equals(Constant.HOST_PUBLISH_PAY)){//车主发布
            j.put("payType",""+Constant.HOST_PUBLISH_FLAG);
            j.put("serviceId",makeupId);
        }else if (from.equals(Constant.HOST_ROB_PAY)){//车主抢单
            j.put("serviceId",makeupId);
            j.put("orderId",orderCode);
            j.put("payType",Constant.HOST_ROB_FLAG+"");
        }else if (from.equals(Constant.PASSENGER_ROB_PAY)){//乘客抢票
            j.put("serviceId",makeupId);
            j.put("orderId",orderCode);
            j.put("payType","16");
        }else if (from.equals(Constant.CHANGE_PAY)){//乘客改签
            j.put("orderId",orderCode);
            j.put("payType","20");
            JSONArray array = new JSONArray();
            for (int i = 0; i < serviceList.size(); i++) {
                Route route = serviceList.get(i);
                JSONObject obj = new JSONObject();
                obj.put("orderId",route.getOrderId());
                obj.put("ownerId",route.getOwnerId());
                obj.put("serviceId",route.getServiceId());
                obj.put("pinTotalAccount",route.getTotalAmount());
                obj.put("passerRequireSeats",route.getPasserRequireSeats());
                //Log.d("changeFormOrder"," pinTotalAccount="+route.getTickets()*hostOrder.getSingle_money()+" passerRequireSeats="+hostOrder.getTickets()+" pinLoseAccount="+hostOrder.getLoss_money());
                array.add(obj);
            }
            j.put("serviceList",array.toString());

        }
        /*
        * {“ownerId”:”12xxxx” , ”serviceId”:”232xxx” , ”pinTotalAccount”:”200” , “passerRequireSeats”:”10”, ”pinLoseAccount” :”100”
        * */

        if (from.equals(Constant.PASSENGER_PUBLISH_PAY)){
            j.put("pinTotalAccount", money);

        }else if (from.equals(Constant.HOST_ROB_PAY)||from.equals(Constant.HOST_PUBLISH_PAY)){
            j.put("pinLoseAccount", money);

        }else if (from.equals(Constant.PASSENGER_ROB_PAY)){
            j.put("pinTotalAccount",money);
        }else if (from.equals(Constant.CHANGE_PAY)){
            j.put("grapAmount",money);
        }else {
            j.put("total_amount", money);

        }

        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                progress.dismiss();
                JSONObject obj = JSON.parseObject(result);
                data = obj.getString("data");
                Log.e("alipay_result",result);
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        PayTask payTask = new PayTask(BookPayActivity.this);
                        Map<String, String> map = payTask.payV2(data, true);
                        Message msg = new Message();
                        msg.what = SDK_PAY_FLAG;
                        msg.obj = map;
                        mHandler.sendMessage(msg);
                    }
                };
                Thread payThread = new Thread(runnable);
                payThread.start();
                BookPayActivity.this.finish();

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progress.dismiss();
                Toast.makeText(BookPayActivity.this, "支付失败,请重新支付", Toast.LENGTH_SHORT).show();
                Log.d("支付宝onError",ex.toString());

            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

    // 支付  输入支付密码
    private void showDialog(final String url) {
        final PayWindow payWindow = new PayWindow(BookPayActivity.this);
        payWindow.showAtLocation(findViewById(R.id.bookpay_layout),Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
        params = getWindow().getAttributes();
        //当弹出Popupwindow时，背景变半透明
        params.alpha=0.7f;
        getWindow().setAttributes(params);
        payWindow.paywinPrice.setText(amount+" 元");
        payWindow.setPayBtOnclick(new PayWindow.PayBtOnclick() {
            @Override
            public void payOnClick(String word) {
                payMoney(word,url);
            }

            @Override
            public void payForgetClick() {
                startActivity(new Intent(BookPayActivity.this,MinePayPasswordActivity.class));
            }
        });
        payWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                params = getWindow().getAttributes();
                params.alpha=1f;
                getWindow().setAttributes(params);
            }
        });
        //设置Popupwindow关闭监听，当Popupwindow关闭，背景恢复1f
        payWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                params = getWindow().getAttributes();
                params.alpha=1f;
                getWindow().setAttributes(params);
            }
        });


    }
    // 余额支付方法
    private void payMoney(String s, final String url) {
        try {
            word = AESUtil.encryptAES(s);

        } catch (Exception e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams(InternetAddressUtis.Url+url);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject j = new JSONObject();

        // 区别对待不同页面
        if (from.equals("between")){
            j.put("payOrderType","07");
            j.put("orderId",makeupId);
        }else if (from.equals("add")){
            j.put("payOrderType","06");
            j.put("makeupId",makeupId);
        }else if (from.equals("hot")){
            j.put("payOrderType","08");
            j.put("orderId",makeupId);
        }else if (from.equals("help")){
            j.put("payOrderType","09");
            j.put("orderId",makeupId);
        }else if (from.equals("reward")){
            j.put("payOrderType","11");
            j.put("rewardId",makeupId);
        }else if (from.equals("buyservice")){
            j.put("payOrderType","12");
            j.put("orderId",makeupId);
        }else if (from.equals("fwadd")){
            j.put("payOrderType","13");
            j.put("makeupId",makeupId);
        }else if (from.equals("service")){
            j.put("payOrderType","02");
            j.put("shopId",makeupId);
        }else if (from.equals(Constant.PASSENGER_PUBLISH_PAY)){//乘客发布
            j.put("payType",Constant.PASSENGER_PUBLISH_FLAG+"");
            j.put("serviceId",makeupId);
            j.put("orderId",orderCode);
        }else if (from.equals(Constant.HOST_PUBLISH_PAY)){//车主发布
            j.put("payType",Constant.HOST_PUBLISH_FLAG+"");
            j.put("serviceId",makeupId);
            j.put("orderId",orderCode);
        }else if (from.equals(Constant.HOST_ROB_PAY)){//车主抢单
            j.put("payType",Constant.HOST_ROB_FLAG+"");
            j.put("serviceId",makeupId);
            j.put("orderId",orderCode);
        }else if (from.equals(Constant.PASSENGER_ROB_PAY)){//乘客购票
            j.put("payType","16");
            j.put("serviceId",makeupId);
            j.put("orderId",orderCode);
        }else if (from.equals(Constant.CHANGE_PAY)){//乘客改签
            j.put("payType","20");
            j.put("orderId",orderCode);
            JSONArray array = new JSONArray();
            for (int i = 0; i < serviceList.size(); i++) {
                Route route = serviceList.get(i);
                JSONObject obj = new JSONObject();
                obj.put("orderId",route.getOrderId());
                obj.put("ownerId",route.getOwnerId());
                obj.put("serviceId",route.getServiceId());
                obj.put("pinTotalAccount",route.getTotalAmount());
                obj.put("passerRequireSeats",route.getPasserRequireSeats());
                //Log.d("changeFormOrder"," pinTotalAccount="+route.getTickets()*hostOrder.getSingle_money()+" passerRequireSeats="+hostOrder.getTickets()+" pinLoseAccount="+hostOrder.getLoss_money());
                array.add(obj);
            }
            j.put("serviceList",array.toString());
        }

        j.put("payPassword", word);
        if (from.equals(Constant.CHANGE_PAY)){
            j.put("grapAmount",amount);

        }else {
            j.put("total_amount",amount);

        }
        Log.d("余额支付","payType="+j.getString("payType")+" serviceId="+makeupId+" orderId="+orderCode+" word="+word+" total_amount"+amount);
        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("yivi_result",result);
                JSONObject object = JSON.parseObject(result);
                String status = object.getString("status");
                if (status.equals("1")){
                    Toast.makeText(BookPayActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                    if (from.equals(Constant.PASSENGER_PUBLISH_PAY)||from.equals(Constant.HOST_PUBLISH_PAY)||from.equals(Constant.CHANGE_PAY)||from.equals(Constant.PASSENGER_ROB_PAY)||from.equals(Constant.HOST_ROB_PAY)){
                        Intent intent = new Intent(BookPayActivity.this, CompleteActivity.class);
                        intent.putExtra("type",completeType);
                        intent.putExtra("text_1",text_1);
                        intent.putExtra("text_2",text_2);
                        Log.i("complete","type="+completeType+"  text1"+text_1+"  text2"+text_2);
                        startActivity(intent);
                    }
                    BookPayActivity.this.finish();

                }else {
                    final String failure = object.getString("failure");
                    if (failure.equals("1016")){
                        AlertDialog.Builder builder = new AlertDialog.Builder(BookPayActivity.this,R.style.NoBackGroundDialog);
                        View view = LayoutInflater.from(BookPayActivity.this).inflate(R.layout.cancel_servicedialog, null);
                        builder.setView(view);
                        Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                        Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                        TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                        t2.setText("余额不足，请充值");
                        final AlertDialog dialog = builder.create();
                        cancelbt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        surebt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(BookPayActivity.this,MineAccountJoinMoney.class));
                                dialog.dismiss();
                            }
                        });
                        dialog.show();

                    }else if (failure.equals("1015")){
                        AlertDialog.Builder builder = new AlertDialog.Builder(BookPayActivity.this,R.style.NoBackGroundDialog);
                        View view = LayoutInflater.from(BookPayActivity.this).inflate(R.layout.cancel_servicedialog, null);
                        builder.setView(view);
                        Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                        Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                        TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                        t2.setText("支付密码错误");

                        final AlertDialog dialog = builder.create();
                        cancelbt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        surebt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                    }else if (failure.equals("1082")){
                        if (from.equals(Constant.PASSENGER_PUBLISH_PAY)||from.equals(Constant.HOST_PUBLISH_PAY)||from.equals(Constant.PASSENGER_ROB_PAY)||from.equals(Constant.HOST_ROB_PAY)){
                            AlertDialog.Builder builder = null;
                            builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
                            builder.setCancelable(true);
                            View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.tip_dialog, null);
                            builder.setView(view);
                            TextView know = (TextView) view.findViewById(R.id.know);
                            TextView message = (TextView) view.findViewById(R.id.dialog_text);
                            know.setText("知道了");
                            message.setText("该订单已超时");
                            final AlertDialog dialog = builder.create();
                            know.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    if (from.equals(Constant.PASSENGER_PUBLISH_PAY)){
                                        Intent intent = new Intent(BookPayActivity.this, ReleaseActivity.class);
                                        intent.putExtra("identity",Constant.PASSENGER);
                                        startActivity(intent);
                                    }else if (failure.equals(Constant.HOST_PUBLISH_PAY)){
                                        Intent intent = new Intent(BookPayActivity.this, ReleaseActivity.class);
                                        intent.putExtra("identity",Constant.HOST);
                                        startActivity(intent);
                                    }/*else if (failure.equals(Constant.PASSENGER_ROB_PAY)){
                                        Intent intent = new Intent(BookPayActivity.this, HostOrdersActivity.class);
                                        intent.putExtra("identity",Constant.PASSENGER);
                                        startActivity(intent);
                                    }else if (failure.equals(Constant.HOST_ROB_PAY)){
                                        Intent intent = new Intent(BookPayActivity.this, PassengerOrdersActivity.class);
                                        intent.putExtra("identity",Constant.HOST);
                                        startActivity(intent);
                                    }*/

                                    dialog.dismiss();
                                }
                            });
                            dialog.show();
                        }
                    }else {
                        Toast.makeText(BookPayActivity.this,"余额支付成功errorCode  else",Toast.LENGTH_SHORT).show();
                        Log.d("余额支付成功",failure);
                    }
                }

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(BookPayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                Log.d("余额支付onError",ex.toString());

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });
    }


}
